package edu.udel.mboch.towerdefense;

import java.util.ArrayList;

public class TimeMove implements TDMove
{
	private static final long serialVersionUID = 3054905648613434604L;
	private long time;
	private boolean spawn;
	private boolean income;
	
	public TimeMove(long time, boolean spawn, boolean income)
	{
		this.time = time;
		this.spawn = spawn;
		this.income = income;
	}
	
	public long getTime()
	{
		return time;
	}
	
	public boolean isValid(TDState state)
	{
		return true;
	}
	
	public void make(TDState state)
	{
		state.getGrid().advanceMobs(time, state);
		state.getGrid().shootTowers(time, state);
		state.getGrid().updateProjectiles(time, state);
		if(spawn)
		{
			if(state.getWaves().getTrueWaveNumber() <= Waves.MAX_WAVES)
			{
				ArrayList<ArrayList<Character>> waves = state.getWaves().getWaves();
				if(waves.get(0).size() == 0)
					waves.remove(0);
				if(waves.size() != 0)
				{
					char mob = waves.get(0).remove(0).charValue();
					if(mob != ' ')
					{
						Position spawn = state.getGrid().getSpawn();
						if(mob == 'B')
							state.getGrid().placeMob(new BasicMob(new FloatingPosition((float)spawn.x(), (float)spawn.y()), state.getGrid().getPath(), state.getPlayer()));
						else if(mob == 'F')
							state.getGrid().placeMob(new FastMob(new FloatingPosition((float)spawn.x(), (float)spawn.y()), state.getGrid().getPath(), state.getPlayer()));
						else if(mob == 'H')
							state.getGrid().placeMob(new HeavyMob(new FloatingPosition((float)spawn.x(), (float)spawn.y()), state.getGrid().getPath(), state.getPlayer()));
						else if(mob == 'X')
							state.getGrid().placeMob(new BossMob(new FloatingPosition((float)spawn.x(), (float)spawn.y()), state.getGrid().getPath(), state.getPlayer()));
					}
					if(mob != ' ' && state instanceof BattleTDState)
					{
						Position spawn2 = ((BattleTDState)state).getGrid().getSpawn2();
						if(mob == 'B')
							state.getGrid().placeMob(new BasicMob(new FloatingPosition((float)spawn2.x(), (float)spawn2.y()), ((BattleTDState)state).getGrid().getPath2(), ((BattleTDState)state).getPlayer2()));
						else if(mob == 'F')
							state.getGrid().placeMob(new FastMob(new FloatingPosition((float)spawn2.x(), (float)spawn2.y()), ((BattleTDState)state).getGrid().getPath2(), ((BattleTDState)state).getPlayer2()));
					}
				}
			}
		}
		if(income && state instanceof BattleTDState)
		{
			state.getPlayer().generateIncome();
			((BattleTDState)state).getPlayer2().generateIncome();
		}
	}
}
